﻿using System;
using System.Collections;
using System.Collections.Generic;
using Data;
using UnityEngine;
public class AccountHandle : IHander
{
    public static event System.Action<Account> OtherLoginHandler;
    public void Register(Dictionary<Protocal, Action<ByteBuffer>> handlers)
    {
        handlers.Add(Protocal.Login_CMD, OnLogin);
        handlers.Add(Protocal.OtherLogin_CMD, OnOtherLogin);
        handlers.Add(Protocal.LogOut_CND, OnLoginOut);
    }
    private void OnLogin(ByteBuffer buffer)
    {
        ReqClient log = ProtoHelper.Deserialize<ReqClient>(buffer.ReadBytes());
        if (log.errorCode.code == 1)
        {
            ActorManager.Instance.localUser = log.localActor;
            ActorManager.Instance.otherUsers = log.actors;
            Application.LoadLevel(1);
        }
        else
        {
            Debug.Log(log.errorCode.error);
        }
    }
    private void OnOtherLogin(ByteBuffer buffer)
    {
        Account log = ProtoHelper.Deserialize<Account>(buffer.ReadBytes());
        OtherLoginHandler(log);
    }
    private void OnLoginOut(ByteBuffer buffer)
    {
        ReqLogin log = ProtoHelper.Deserialize<ReqLogin>(buffer.ReadBytes());
        Debug.Log(log.nickname + "退出游戏");
        ActorManager.Instance.RemoveTank(log.username);
    }
}
